package net.adst.ui.mainframe.tabs;

import net.adst.beans.NaemTec;
import net.adst.beans.OurTecRow;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.ui.mainframe.tabs.nanemTap.NanemPanel;
import net.adst.ui.mainframe.tabs.ourTab.OurPanel;
import net.adst.util.DataManager;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 3:55
 * To change this template use File | Settings | File Templates.
 */
public class AdstTabPanel extends JTabbedPane {


    private static int index = 0;

    public AdstTabPanel() {
        addTabbedPane();
        setColors();
    }

    private void setColors() {
        setBackground(Constants.COLOR_LIGHT_BLUE);
        Component[] components = getComponents();
        for (Component comp : components) {
            Component[] panelComponents = ((Container) comp).getComponents();
            for (Component compPanel : panelComponents) {
                Component[] settingsIn = ((Container) compPanel).getComponents();
                for (Component settIn : settingsIn) {
                    Component[] settingsInIn = ((Container) settIn).getComponents();
                    for (Component settInIn : settingsInIn) {
                        if (!(settInIn instanceof JComboBox) && !(settInIn instanceof JTextField) && !(settInIn instanceof JViewport)) {
                            settInIn.setBackground(Constants.COLOR_DARK_BLUE);
                        }
                    }
                    if (!(settIn instanceof JComboBox) && !(settIn instanceof JTextField)) {
                        settIn.setBackground(Constants.COLOR_DARK_BLUE);
                    }
                }
                compPanel.setBackground(Constants.COLOR_DARK_BLUE);
            }
            comp.setBackground(Constants.COLOR_DARK_BLUE);
        }
        addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
//                MainFrame frame = (MainFrame) Frame.getFrames()[0];

//                setDatesToPeriodChooserArea();
//                setTimeFormat();
                index = getSelectedIndex();
                //ORS-211
//                ((JDateChooser) DataManager.getObject(ResourceManager.getVariableName("ui.topPanel.reportDateArea.task.dateChooser"))).setDate(Calendar.getInstance().getTime());
                final JTextArea description = (JTextArea) DataManager.getObject(VariableNames.COMMENT_TEXT);
                JTable selTable;

                switch (index) {
                    case 0:
                        selTable = (JTable) DataManager.getObject(VariableNames.UI_NAEM_DETAILED_TABLE);
                        if (selTable.getSelectedRow() > -1) {
                            ArrayList<NaemTec> rows = (ArrayList<NaemTec>) DataManager.getObject(VariableNames.TEC_NAEM_USING_ROWS);
                            NaemTec tec = rows.get(selTable.getSelectedRow());
                            description.setText(tec.getComment());
                            description.setEnabled(true);
                            description.setEditable(true);
                            ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_REMOVE_BUTTON)).setEnabled(true);
                        } else {
                            description.setText("");
                            description.setEnabled(true);
                            description.setEditable(true);
                            ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_REMOVE_BUTTON)).setEnabled(false);
                        }
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_NAEM_PAID_NAM_BUTTON)).setVisible(true);
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_NAEM_PAID_NAM_OK_BUTTON)).setVisible(true);
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_NAEM_PAID_NAM_NADO_BUTTON)).setVisible(true);
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_OUR_BUTTON)).setVisible(false);
                        index = 0;
                        break;
                    case 1:
                        selTable = (JTable) DataManager.getObject(VariableNames.UI_OUR_DETAILED_TABLE);
                        if (selTable.getSelectedRow() > -1) {
                            ArrayList<OurTecRow> rows = (ArrayList<OurTecRow>) DataManager.getObject(VariableNames.TEC_OUR_USING_ROWS);
                            description.setText(rows.get(selTable.getSelectedRow()).getComment());
                            description.setEnabled(true);
                            description.setEditable(true);
                            ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_REMOVE_BUTTON)).setEnabled(true);
                        } else {
                            description.setText("");
                            description.setEnabled(false);
                            description.setEditable(false);
                            ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_REMOVE_BUTTON)).setEnabled(false);
                        }
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_NAEM_PAID_NAM_BUTTON)).setVisible(false);
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_NAEM_PAID_NAM_OK_BUTTON)).setVisible(false);
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_NAEM_PAID_NAM_NADO_BUTTON)).setVisible(false);
                        ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_OUR_BUTTON)).setVisible(true);
                        index = 1;
                        break;
                }
//                setTableFocus();
            }
        });
    }

    /**
     * Adds tabs (Home, Favourite, Reports, Settings)
     */
    private void addTabbedPane() {
        addTab("нанимаемая техника", new NanemPanel());
        addTab("наша техника", new OurPanel());

        addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                index = getSelectedIndex();
            }
        });
        setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    public static int getIndex() {
        return index;
    }
}
